package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzLikeMembersActivity;
import com.edgclub.edg.activity.GzWebViewActivity;
import com.edgclub.edg.activity.UserInfoOtherActivity;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.constant.BBConstant;
import com.edgclub.edg.model.PostsAlbum;
import com.edgclub.edg.model.Reply;
import com.edgclub.edg.model.Topic;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.Userspan;
import com.edgclub.edg.model.result.Rewards;
import com.edgclub.edg.uc.SpaceItemDecoration;
import com.edgclub.edg.uc.TextViewFixTouchConsume;
import com.edgclub.edg.uc.UnableScrollGridView;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.DateUtils;
import com.edgclub.edg.utils.util.DensityUtils;
import com.edgclub.edg.utils.util.MediaPlayerUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.rockerhieu.emojicon.EmojiconTextView1;

import java.util.ArrayList;

/**
 */
public class BBDetailAdapter extends GzBaseAdapter{
    private Context mContext;
    private GlideUtils glideUtils;
    private User user;
    private boolean isMyTopic = false;

    private Topic topic;
    private ArrayList<User> likeUsers;//点赞用户
    private ArrayList<Reply> detailDatas;//评论
    private Rewards rewards;//打赏
    private int hotReplyCount=0;
    private MediaPlayerUtil mediaPlayerUtil;
    private AnimationSet set;
    private AnimationDrawable voiceAnimation = null;

    private int mColor,mColor2;
    private ReplyOnclickListener replyOnclickListener;
    private OpenRedClickListener openRedClickListener;

    private int spacingInPixels;

    private static final int detail_head = 0;
    private static final int detail_reward = 1;
    private static final int detail_like = 2;
    private static final int detail_empty = 3;
    private static final int detail_comment = 4;

    private LikeAdapter likeUserAdapter;

    private RewardAdapter rewardAdapter;
    private RewardAdapter.onAddRewardClickListener addRewardClickListener;

    public BBDetailAdapter(Context context) {
        this.mContext = context;
        glideUtils = new GlideUtils();
        detailDatas = new ArrayList<>();
        user = new UserPrefUtils(mContext).getUser();
        mColor = mContext.getResources().getColor(R.color.c1);
        mColor2 = mContext.getResources().getColor(R.color.c6);
        spacingInPixels = DensityUtils.dip2px(mContext,12);
        mediaPlayerUtil = new MediaPlayerUtil(mContext);

        set = new AnimationSet(true);
        AlphaAnimation alph1 = new AlphaAnimation(0.2f, 1);
        alph1.setDuration(600);
        AlphaAnimation alph2 = new AlphaAnimation(1f, 0.4f);
        alph2.setDuration(600);
        alph2.setStartOffset(600);
        set.addAnimation(alph1);
        set.addAnimation(alph2);
        set.setRepeatCount(500);
        set.setFillAfter(true);

    }

    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if (viewType == detail_head) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.head_bb_detail,viewGroup,false);
            return new HeadViewHolder(view,mListener,mLongClickListener);
        } else if(viewType == detail_reward){
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_bb_detail_reward,viewGroup,false);
            return new RewardViewHolder(view,mListener,mLongClickListener);
        }else if(viewType == detail_like){
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.head_bb_detail_likes,viewGroup,false);
            return new LikeViewHolder(view,mListener,mLongClickListener);
        } else if(viewType == detail_empty){
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_gz_empty,viewGroup,false);
            return new EmptyViewHolder(view,mListener,mLongClickListener);
        } else{
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_bb_detail,viewGroup,false);
            return new ViewHolder(view,mListener,mLongClickListener);
        }
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder1, final int position) {
        switch (getItemViewType(position)){
            case detail_head:
                BBDetailAdapter.HeadViewHolder headViewHolder= (HeadViewHolder) viewHolder1;
                setHeadContent(headViewHolder,topic);
                break;
            case detail_reward:
                BBDetailAdapter.RewardViewHolder rewardViewHolder= (RewardViewHolder) viewHolder1;
                setRewards(rewardViewHolder,rewards);
                break;
            case detail_like:
                BBDetailAdapter.LikeViewHolder likeViewHolder= (LikeViewHolder) viewHolder1;
                setLikeUsers(likeViewHolder,likeUsers);
                break;
            case detail_empty:
                BBDetailAdapter.EmptyViewHolder emptyViewHolder = (EmptyViewHolder) viewHolder1;
                emptyViewHolder.imageView_empty.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(replyOnclickListener!=null){
                            replyOnclickListener.replyTopic();
                        }
                    }
                });
                break;
            case detail_comment:
                final BBDetailAdapter.ViewHolder viewHolder= (ViewHolder) viewHolder1;
                final int pos = position-(getItemCount()-detailDatas.size());
                final Reply reply =  detailDatas.get(pos);
                if(reply.getUser()!=null){
                    viewHolder.user_name.setText(reply.getUser().getNick()+"");
                    if(reply.getUser().getId().equals(topic.getUser().getId())){
                        viewHolder.tv_owner.setVisibility(View.VISIBLE);
                    }else{
                        viewHolder.tv_owner.setVisibility(View.GONE);
                    }
                    glideUtils.Glidde_loadCropCircleImage(mContext,reply.getUser().getLogo(),viewHolder.user_icon);
                    viewHolder.user_icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent=new Intent(mContext, UserInfoOtherActivity.class);
                            intent.putExtra("user",reply.getUser());
                            mContext.startActivity(intent);
                        }
                    });
                    if(null!=reply.getUser().getUserAuth()){
                        if ("-1".equals(reply.getUser().getUserAuth().getType())){
                            viewHolder.isvip.setVisibility(View.GONE);
                        }
                        else if("0".equals(reply.getUser().getUserAuth().getType())){
                            viewHolder.isvip.setVisibility(View.VISIBLE);
                            viewHolder.isvip.setImageResource(R.drawable.v_huan);
                        }else if("1".equals(reply.getUser().getUserAuth().getType())){
                            viewHolder.isvip.setVisibility(View.VISIBLE);
                            viewHolder.isvip.setImageResource(R.drawable.v_lan);
                        }
                    }else{
                        viewHolder.isvip.setVisibility(View.GONE);
                    }
                }

                //是否显示热门评论
                viewHolder.ll_all_line.setVisibility(View.GONE);
                viewHolder.ll_hot_line.setVisibility(View.GONE);
                if (pos == 0 && hotReplyCount!=0 ) {
                    viewHolder.ll_hot_line.setVisibility(View.VISIBLE);
                }
                if(pos == hotReplyCount){
                    viewHolder.ll_all_line.setVisibility(View.VISIBLE);
                }

                String num = reply.getFloor()==null?"1":reply.getFloor();
                viewHolder.reply_time.setText(num
                        + "楼"+"  "
                        + DateUtils.getTimestampStringBB(reply.getUpdateAt()));
                /*****************内容*****************/
                viewHolder.topic_content.setVisibility(View.VISIBLE);
                if(TextUtils.isEmpty(reply.getContent())){
                    viewHolder.topic_content.setVisibility(View.GONE);
                }
                viewHolder.topic_content.setText(reply.getContent());
                viewHolder.topic_content.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        replyOnclickListener.reply(pos);
                    }
                });

                viewHolder.tvreply1.setVisibility(View.GONE);
                viewHolder.tvreply2.setVisibility(View.GONE);
                viewHolder.tv_more_reply.setVisibility(View.GONE);
                viewHolder.imgv_line.setVisibility(View.GONE);
                if(reply.getReplys()!=null) {
                    if (reply.getReplys().size() > 0) {
                        final Reply reply1 = reply.getReplys().get(0);
                        boolean isOwner = false;
                        if(topic.getUser().getId().equals(reply1.getUser().getId())){
                            isOwner = true;
                        }
                        viewHolder.tvreply1.setVisibility(View.VISIBLE);
                        viewHolder.tvreply1.setText(getUserSpanString(reply1, isOwner));
                        viewHolder.imgv_line.setVisibility(View.VISIBLE);
                        viewHolder.tvreply1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                replyOnclickListener.replyComment(pos,reply1);
                            }
                        });
                    }
                    if (reply.getReplys().size() > 1) {
                        final Reply reply2 = reply.getReplys().get(1);
                        boolean isOwner = false;
                        if(topic.getUser().getId().equals(reply2.getUser().getId())){
                            isOwner = true;
                        }
                        viewHolder.tvreply2.setVisibility(View.VISIBLE);
                        viewHolder.tvreply2.setText(getUserSpanString(reply2, isOwner));
                        viewHolder.imgv_line.setVisibility(View.VISIBLE);
                        viewHolder.tvreply2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                replyOnclickListener.replyComment(pos,reply2);
                            }
                        });
                    }
                }
                if(reply.getRemainReplys()>2){
                    viewHolder.imgv_line.setVisibility(View.VISIBLE);
                    viewHolder.tv_more_reply.setVisibility(View.VISIBLE);
                    viewHolder.tv_more_reply.setText("更多"+(reply.getRemainReplys()-2)+"条回复...");
                    viewHolder.tv_more_reply.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(replyOnclickListener!=null){
                                replyOnclickListener.more_reply(pos,reply);
                            }
                        }
                    });
                }

                /*****************图片*****************/
                viewHolder.unableScrollRecycleView.setVisibility(View.VISIBLE);
                ArrayList<PostsAlbum> albums = reply.getAlbums();
                if (albums != null && albums.size() > 0) {
                    BBDetailImageAdapter imageAdapter = new BBDetailImageAdapter(mContext);
                    viewHolder.unableScrollRecycleView.setAdapter(imageAdapter);
                    int ww = (int) mContext.getResources().getDimension(R.dimen.size_90);
                    int tmpW = DensityUtils.getScreenW((Activity) mContext) - ww;
                    int totalHeight = 0;
                    for(int i = 0;i<albums.size();i++){
                        PostsAlbum album = albums.get(i);
                        int width = album.getWidth();
                        int height = album.getHeight();
                        if(width==0 || height == 0){
                            album.setWidth(tmpW);
                            album.setHeight(tmpW);
                        }else{
                            if(width>tmpW){
                                album.setWidth(tmpW);
                            }else{
                                album.setWidth(width);
                            }
                            album.setHeight(album.getWidth()*height/width);
                            if(album.getHeight()>3000){
                                album.setWidth(3000*album.getWidth()/album.getHeight());
                                album.setHeight(3000);
                            }
                        }
                        totalHeight = totalHeight+album.getHeight();
                    }
                    ViewGroup.LayoutParams params =  viewHolder.unableScrollRecycleView.getLayoutParams();
                    params.width = tmpW;
                    params.height = totalHeight+(albums.size()-1)*spacingInPixels;
                    viewHolder.unableScrollRecycleView.setLayoutParams(params);
                    imageAdapter.setPostsAlbums(albums);
                    imageAdapter.notifyDataSetChanged();

                } else {
                    viewHolder.unableScrollRecycleView.setVisibility(View.GONE);
                }

                viewHolder.even_more.setOnClickListener(new MyOnClickListener(pos));

                //评论点赞数
                if(reply.getLikes()>0){
                    viewHolder.tv_zan_count.setVisibility(View.VISIBLE);
                    viewHolder.tv_zan_count.setText(reply.getLikes()+"");
                }else{
                    viewHolder.tv_zan_count.setVisibility(View.GONE);
                }
                if(reply.isLiked()){
                    viewHolder.imgv_zan.setSelected(true);
                }else{
                    viewHolder.imgv_zan.setSelected(false);
                }
                viewHolder.order.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewHolder.order.setSelected(!viewHolder.order.isSelected());
                        if(replyOnclickListener!=null){
                            replyOnclickListener.order();
                        }
                    }
                });
                viewHolder.imgv_zan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(replyOnclickListener!=null){
                            replyOnclickListener.like(pos,viewHolder.imgv_zan);
                        }
                    }
                });
                viewHolder.tvreply1
                        .setMovementMethod(TextViewFixTouchConsume.LocalLinkMovementMethod
                                .getInstance());
                viewHolder.tvreply2
                        .setMovementMethod(TextViewFixTouchConsume.LocalLinkMovementMethod
                                .getInstance());



        break;
        }


    }



    private void setHeadContent(HeadViewHolder headViewHolder,final Topic topic){
        if(topic.getUser()!=null){
            headViewHolder.user_name.setText(topic.getUser().getNick()+"");
            glideUtils.Glidde_loadCropCircleImage(mContext,topic.getUser().getLogo(),headViewHolder.user_icon);
            if(user.getId().equals(topic.getUser().getId())){
                isMyTopic = true;
            }
            headViewHolder.user_icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(mContext, UserInfoOtherActivity.class);
                    intent.putExtra("user",topic.getUser());
                    mContext.startActivity(intent);
                }
            });
            if(topic.getUser().getUserAuth()!=null){
                if("-1".equals(topic.getUser().getUserAuth().getType())){
                    headViewHolder.isvip.setVisibility(View.GONE);
                }
                else if("0".equals(topic.getUser().getUserAuth().getType())){
                    headViewHolder.isvip.setVisibility(View.VISIBLE);
                    headViewHolder.isvip.setImageResource(R.drawable.v_huan);
                }else if("1".equals(topic.getUser().getUserAuth().getType())){
                    headViewHolder.isvip.setVisibility(View.VISIBLE);
                    headViewHolder.isvip.setImageResource(R.drawable.v_lan);
                }
            }else{
                headViewHolder.isvip.setVisibility(View.GONE);
            }
        }
        headViewHolder.topic_time.setText(DateUtils.getTimestampStringBB(Long.valueOf(topic.getCreateAt())));
//        headViewHolder.topic_time.setText(com.hyphenate.easeui.utils.DateUtils.getTimestampString(new Date(Long.valueOf(topic.getCreateAt()))));
        /*****************内容*****************/
        headViewHolder.topic_content.setVisibility(View.VISIBLE);
        if(TextUtils.isEmpty(topic.getContent())){
            headViewHolder.topic_content.setVisibility(View.GONE);
        }
        headViewHolder.topic_content.setText(topic.getContent(),new Intent(mContext, GzWebViewActivity.class));

        /*****************位置*****************/
        if (topic.getShowLoc() != null && topic.getShowLoc().equals("1") && !"".equals(topic.getLocation())) {
            headViewHolder.topic_locaiton_layout.setVisibility(View.VISIBLE);
            headViewHolder.topic_city.setText(topic.getLocation());
        } else {
            headViewHolder.topic_locaiton_layout.setVisibility(View.GONE);
        }

        /**************** 红包 *********************/
        if(!TextUtils.isEmpty(topic.getRedId())){
            headViewHolder.ll_red_packet.setVisibility(View.VISIBLE);
            headViewHolder.ll_red_packet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(openRedClickListener!=null)
                        openRedClickListener.OpenRedDialog();
//                    Intent i = new Intent(this, GzRedOpenDetailActivity.class);
//                    i.putExtra("redId",topic.getRedId());
//                    this.startActivity(i);
                }
            });
        }else{
            headViewHolder.ll_red_packet.setVisibility(View.GONE);
        }

        //分离图片语音
        ArrayList<PostsAlbum> audios = new ArrayList<>();
        ArrayList<PostsAlbum> albums = new ArrayList<>();
        final ArrayList<PostsAlbum> tmpalbums = topic.getAlbums();
        if (tmpalbums != null && tmpalbums.size() > 0) {
            for (PostsAlbum postsAlbum : tmpalbums) {
                if (postsAlbum.getUrl().contains(
                        BBConstant.URL_VOICE_PROTOCOL)) {
                    audios.add(postsAlbum);
                }else if(postsAlbum.getUrl().contains(BBConstant.URL_VIDEO_PROTOCOL)){
                    topic.setVideo(postsAlbum);
                }else{
                    albums.add(postsAlbum);
                }
            }
        }
        topic.setAlbums(albums);
        topic.setAudios(audios);
        /**************** 语音 *********************/
        headViewHolder.ll_voice.setVisibility(View.GONE);
        if (audios != null && audios.size() > 0) {
            headViewHolder.ll_voice.setVisibility(View.VISIBLE);
            playVoice(headViewHolder.iv_voice_bg, headViewHolder.iv_voice,headViewHolder.tv_length,audios.get(0).getUrl(), audios.get(0)
                    .getThumbnail());
        }
        /*****************图片*****************/
        headViewHolder.unableScrollRecycleView.setVisibility(View.VISIBLE);
        if (albums != null && albums.size() > 0) {
            BBDetailImageAdapter imageAdapter = new BBDetailImageAdapter(mContext);
            headViewHolder.unableScrollRecycleView.setAdapter(imageAdapter);
            int ww = (int) mContext.getResources().getDimension(R.dimen.size_34);
            int tmpW = DensityUtils.getScreenW((Activity) mContext) - ww;
            int totalHeight = 0;
            for(int i = 0;i<albums.size();i++){
                PostsAlbum album = albums.get(i);
                int width = album.getWidth();
                int height = album.getHeight();
                if(width==0 || height == 0){
                    album.setWidth(tmpW);
                    album.setHeight(tmpW);
                }else{
                    if(width>tmpW){
                        album.setWidth(tmpW);
                    }else{
                        album.setWidth(width);
                    }
                    album.setHeight(album.getWidth()*height/width);
                }
                totalHeight = totalHeight+album.getHeight();
            }
            ViewGroup.LayoutParams params = headViewHolder.unableScrollRecycleView.getLayoutParams();
            params.width = tmpW;
            params.height = totalHeight+(albums.size()-1)*spacingInPixels;
            headViewHolder.unableScrollRecycleView.setLayoutParams(params);
            imageAdapter.setPostsAlbums(albums);
            imageAdapter.notifyDataSetChanged();

        } else {
            headViewHolder.unableScrollRecycleView.setVisibility(View.GONE);
        }
    }

    private void setLikeUsers(LikeViewHolder likeViewHolder,ArrayList<User> likeUsers){
        if (likeUsers != null && likeUsers.size() > 0) {
            likeViewHolder.ll_like.setVisibility(View.VISIBLE);
            likeUserAdapter.setLikeUsers(likeUsers);
            likeUserAdapter.notifyDataSetChanged();
            likeViewHolder.tv_like_num.setText(topic.getLikes() + "人赞过");
            likeViewHolder.tv_like_num.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent1 = new Intent(mContext,GzLikeMembersActivity.class);
                    intent1.putExtra("topicId",topic.getPostsId());
                    mContext.startActivity(intent1);
                }
            });
            likeViewHolder.ll_like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent1 = new Intent(mContext,GzLikeMembersActivity.class);
                    intent1.putExtra("topicId",topic.getPostsId());
                    mContext.startActivity(intent1);
                }
            });
            likeViewHolder.likeRecycleView
                    .setOnTouchBlankPositionListener(new UnableScrollGridView.OnTouchBlankPositionListener() {

                        @Override
                        public boolean onTouchBlankPosition() {
                            Intent intent1 = new Intent(mContext,GzLikeMembersActivity.class);
                            intent1.putExtra("topicId",topic.getPostsId());
                            mContext.startActivity(intent1);
                            return true;
                        }
                    });
        } else {
            likeViewHolder.ll_like.setVisibility(View.GONE);
        }
    }

    private void setRewards(RewardViewHolder rewardViewHolder,Rewards rewards){
        if (rewards != null) {
            rewardViewHolder.ll_reward.setVisibility(View.VISIBLE);
            rewardViewHolder.reward_gridView.setVisibility(View.GONE);
            if(rewards.getUsers()!=null && rewards.getUsers().size()>0){
                rewardViewHolder.reward_gridView.setVisibility(View.VISIBLE);
                rewardAdapter.setRewardUsers(rewards.getUsers());
                rewardAdapter.setAddRewardClickListener(addRewardClickListener);
                rewardAdapter.notifyDataSetChanged();
            }
            if(rewards.getGold()>0){
                rewardViewHolder.tv_reward_num.setVisibility(View.VISIBLE);
                rewardViewHolder.tv_reward_num.setText("已打赏"+rewards.getGold());
            }else{
                rewardViewHolder.tv_reward_num.setVisibility(View.GONE);
            }

            rewardViewHolder.tv_reward_more.setText(rewards.getNumber()+"人打赏过");
//            rewardViewHolder.tv_reward_more.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent1 = new Intent(mContext,RewardListActivity.class);
//                    intent1.putExtra("topicId",topic.getPostsId());
//                    mContext.startActivity(intent1);
//                }
//            });
        } else {
            rewardViewHolder.ll_reward.setVisibility(View.GONE);
        }
    }


    class MyOnClickListener implements View.OnClickListener {
        private int position;

        public MyOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.even_more:
                    if(replyOnclickListener!=null){
                        replyOnclickListener.even_more(position);
                    }
                    break;
            }

        }
    }


    private SpannableString getUserSpanString(Reply reply, boolean isOwner) {

        String time = "";
        try {
            time = DateUtils
                    .getTimestampStringBB(Long.valueOf(reply.getUpdateAt()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String owner = "";
//        if(isOwner){
//            owner = " 楼主 ";
//        }
        String replyString =  reply.getUser().getNick()+owner+": "
                + reply.getContent()+"   "+time;
        int timelength = time.length();

        User user = new User();
        String userNick = reply.getUser().getNick();
        user.setId(reply.getUser().getId());

        Userspan userspan = new Userspan("123", mColor, mContext, user);

        SpannableString ss = new SpannableString(replyString);

        ss.setSpan(userspan, 0,  userNick.length()+owner.length()+1,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//        if(!TextUtils.isEmpty(owner)){
//            //得到drawable对象，即所要插入的图片
//            Drawable d = mContext.getResources().getDrawable(R.drawable.owner_bg);
//            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
//            ss.setSpan(span, userNick.length()+1, userNick.length()+owner.length()-1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//        }

        ss.setSpan(new ForegroundColorSpan(mColor2), userNick.length()+owner.length()+1,
                replyString.length()-timelength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(DensityUtils.sp2px(mContext,13)), replyString.length()-timelength, replyString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return ss;
    }


    //获取数据的数量
    @Override
    public int getItemCount() {
        int size = detailDatas.size();
        if(topic!=null){
            size = size+1;
        }
        if(rewards!=null){
            size = size+1;
        }
        if(likeUsers!=null && likeUsers.size()>0){
            size = size+1;
        }
        if(size>0 && (detailDatas==null || detailDatas.size()==0)){
            size = size+1;
        }

        return size;
    }

    @Override
    public int getItemViewType(int position) {

        if(topic!=null && position==0){
            return detail_head;
        } else if(rewards!=null && position==1){
            return detail_reward;
        } else if(likeUsers!=null && likeUsers.size()>0 && rewards==null && position==1){
            return detail_like;
        }else if(likeUsers!=null && likeUsers.size()>0 && rewards!=null && position==2){
            return detail_like;
        }else if(detailDatas==null || detailDatas.size() ==0){
            return detail_empty;
        }else{
            return detail_comment;
        }
    }

    public ArrayList<Reply> getDetailDatas() {
        return detailDatas;
    }

    public void setDetailDatas(ArrayList<Reply> detailDatas) {
        this.detailDatas = detailDatas;
    }

    public Topic getTopic() {
        return topic;
    }

    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    public ArrayList<User> getLikeUsers() {
        return likeUsers;
    }

    public void setLikeUsers(ArrayList<User> likeUsers) {
        this.likeUsers = likeUsers;
    }

    public Rewards getRewards() {
        return rewards;
    }

    public void setRewards(Rewards rewards) {
        this.rewards = rewards;
    }

    public int getHotReplyCount() {
        return hotReplyCount;
    }

    public void setHotReplyCount(int hotReplyCount) {
        this.hotReplyCount = hotReplyCount;
    }

    public ReplyOnclickListener getReplyOnclickListener() {
        return replyOnclickListener;
    }

    public void setReplyOnclickListener(ReplyOnclickListener replyOnclickListener) {
        this.replyOnclickListener = replyOnclickListener;
    }

    /**
     * 播放语音
     *
     */
    private void playVoice(final ImageView iv_voice_bg,
                           final ImageView iv_voice, TextView tv_length, final String urlPro,
                           final String thumbnail) {
        int lenght = 1;
        try {
            lenght = Integer
                    .parseInt(urlPro.substring(urlPro.lastIndexOf("/") + 1));
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 设置语音消息的背景
        ViewGroup.LayoutParams para = iv_voice_bg.getLayoutParams();
        Resources r = mContext.getResources();
        int min = (int) r.getDimension(R.dimen.size_50);
        int max = (int) r.getDimension(R.dimen.size_150);
        int plus = (max - min) * lenght / 60;
        para.width = min + plus;
        iv_voice_bg.setLayoutParams(para);

        tv_length.setText(lenght + "\"");

        if (mediaPlayerUtil.isPlaying()) {
            iv_voice.setImageResource(R.drawable.voice_to_play_topic_list);
            voiceAnimation = (AnimationDrawable) iv_voice.getDrawable();
            voiceAnimation.start();
        } else {
            // if(voiceAnimation!=null &&
            // voiceAnimation.isRunning())voiceAnimation.stop();
            iv_voice.setImageResource(R.drawable.gz_topic_voice_playing_f3);
        }

        iv_voice_bg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (mediaPlayerUtil.isPlaying()) {
                    if (voiceAnimation != null && voiceAnimation.isRunning())
                        voiceAnimation.stop();
                    iv_voice.setImageResource(R.drawable.gz_topic_voice_playing_f3);
                    mediaPlayerUtil.stopPlayVoice();
                } else {
                    iv_voice.setVisibility(View.GONE);
                    iv_voice_bg.clearAnimation();
                    iv_voice_bg.setAnimation(set);
                    set.start();

                    mediaPlayerUtil.playVoice(thumbnail);

                    mediaPlayerUtil.getMediaPlayer().setOnCompletionListener(
                            new MediaPlayer.OnCompletionListener() {

                                @Override
                                public void onCompletion(MediaPlayer mp) {
                                    // TODO Auto-generated method stub
                                    voiceAnimation.stop();
                                    mediaPlayerUtil.stopPlayVoice();
                                    iv_voice.setImageResource(R.drawable.gz_topic_voice_playing_f3);
                                }
                            });
                    mediaPlayerUtil.getMediaPlayer().setOnPreparedListener(
                            new MediaPlayer.OnPreparedListener() {

                                @Override
                                public void onPrepared(MediaPlayer mp) {
                                    // TODO Auto-generated method stub
                                    iv_voice.setVisibility(View.VISIBLE);
                                    iv_voice.setImageResource(R.drawable.voice_to_play_topic_list);
                                    voiceAnimation = (AnimationDrawable) iv_voice
                                            .getDrawable();
                                    iv_voice.clearAnimation();
                                    voiceAnimation.start();

                                    iv_voice_bg.clearAnimation();

                                }
                            });
                }

            }
        });
    }

    public MediaPlayerUtil getMediaPlayerUtil() {
        return mediaPlayerUtil;
    }

    public void setMediaPlayerUtil(MediaPlayerUtil mediaPlayerUtil) {
        this.mediaPlayerUtil = mediaPlayerUtil;
    }

    public OpenRedClickListener getOpenRedClickListener() {
        return openRedClickListener;
    }

    public void setOpenRedClickListener(OpenRedClickListener openRedClickListener) {
        this.openRedClickListener = openRedClickListener;
    }

    public RewardAdapter.onAddRewardClickListener getAddRewardClickListener() {
        return addRewardClickListener;
    }

    public void setAddRewardClickListener(RewardAdapter.onAddRewardClickListener addRewardClickListener) {
        this.addRewardClickListener = addRewardClickListener;
    }

    public interface ReplyOnclickListener {
        public void even_more(int position);

        public void reply(int position);//回复

        public void replyComment(int position,Reply reply);//回复的回复

        public void more_reply(int position,Reply reply);

        public void replyTopic();//点击沙发回复帖子

        public void like(int position,ImageView imgv_zan );//回复点赞

        public void order();//回复点赞
    }

    public interface OpenRedClickListener{
        public void OpenRedDialog();//点击抢红包
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RViewHolder {
        private EmojiconTextView1 topic_content;
        private EmojiconTextView1 tvreply1,tvreply2;
        private TextView reply_time,user_name,tv_more_reply,tv_zan_count;
        private ImageView user_icon,even_more,imgv_line,isvip,tv_owner,imgv_zan,order;
        private RecyclerView unableScrollRecycleView;

        //热门评论 全部评论
        private RelativeLayout ll_hot_line;
        private LinearLayout ll_all_line;

        public ViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            topic_content = (EmojiconTextView1) view.findViewById(R.id.topic_content);
            tvreply1 = (EmojiconTextView1) view.findViewById(R.id.tvreply1);
            tvreply2 = (EmojiconTextView1) view.findViewById(R.id.tvreply2);
            reply_time = (TextView) view.findViewById(R.id.reply_time);
            user_name = (TextView) view.findViewById(R.id.user_name);
            tv_owner = (ImageView) view.findViewById(R.id.tv_owner);
            user_icon = (ImageView) view.findViewById(R.id.user_icon);
            tv_more_reply = (TextView) view.findViewById(R.id.tv_more_reply);
            tv_zan_count = (TextView) view.findViewById(R.id.tv_zan_count);
            unableScrollRecycleView = (RecyclerView) view.findViewById(R.id.unableScrollRecycleView);
            even_more = (ImageView) view.findViewById(R.id.even_more);
            imgv_zan = (ImageView) view.findViewById(R.id.imgv_zan);
            imgv_line = (ImageView) view.findViewById(R.id.imgv_line);
            isvip = (ImageView) view.findViewById(R.id.isvip);
            order = (ImageView) view.findViewById(R.id.order);
            ll_hot_line = (RelativeLayout) view.findViewById(R.id.ll_hot_line);
            ll_all_line = (LinearLayout) view.findViewById(R.id.ll_all_line);

            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            unableScrollRecycleView.setLayoutManager(layoutManager);
            unableScrollRecycleView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));

        }
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class HeadViewHolder extends RViewHolder {
        private EmojiconTextView1 topic_content;
        private TextView topic_time,user_name;
        private ImageView user_icon , isvip;
        private RecyclerView unableScrollRecycleView;
        private RelativeLayout ll_red_packet;
        private TextView tv_red_packet;
        //语音
        ImageView iv_voice, iv_voice_bg;
        RelativeLayout ll_voice;
        TextView tv_length;
        private MediaPlayerUtil mediaPlayerUtil;
        private AnimationSet set;
        private AnimationDrawable voiceAnimation = null;
        //位置
        TextView topic_city;
        LinearLayout topic_locaiton_layout;


        public HeadViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            topic_content = (EmojiconTextView1) view.findViewById(R.id.topic_content);
            topic_time = (TextView) view.findViewById(R.id.topic_time);
            user_name = (TextView) view.findViewById(R.id.user_name);
            user_icon = (ImageView) view.findViewById(R.id.user_icon);
            isvip = (ImageView) view.findViewById(R.id.isvip);

            unableScrollRecycleView = (RecyclerView) view.findViewById(R.id.unableScrollRecycleView);
            iv_voice = (ImageView) view.findViewById(R.id.iv_voice);
            iv_voice_bg = (ImageView) view.findViewById(R.id.iv_voice_bg);
            ll_voice = (RelativeLayout) view.findViewById(R.id.ll_voice);
            tv_length = (TextView) view.findViewById(R.id.tv_length);
            topic_city = (TextView) view.findViewById(R.id.topic_city);
            topic_locaiton_layout = (LinearLayout) view.findViewById(R.id.topic_locaiton_layout);
            ll_red_packet = (RelativeLayout) view.findViewById(R.id.ll_red_packet);
            tv_red_packet = (TextView) view.findViewById(R.id.tv_red_packet);

            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            unableScrollRecycleView.setLayoutManager(layoutManager);
            unableScrollRecycleView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
        }
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class LikeViewHolder extends RViewHolder {
        private RelativeLayout ll_like;
        private UnableScrollGridView likeRecycleView;
        private TextView tv_like_num;


        public LikeViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);

            ll_like = (RelativeLayout) view.findViewById(R.id.ll_like);
            likeRecycleView = (UnableScrollGridView) view.findViewById(R.id.likeRecycleView);
            tv_like_num = (TextView) view.findViewById(R.id.tv_like_num);
            tv_like_num.setOnClickListener(this);

//            LinearLayoutManager layoutManager2 = new LinearLayoutManager(mContext);
//            likeRecycleView.setLayoutManager(layoutManager2);
            likeUserAdapter = new LikeAdapter(mContext);
            likeRecycleView.setAdapter(likeUserAdapter);
        }
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class RewardViewHolder extends RViewHolder {
        private RelativeLayout ll_reward;
        private UnableScrollGridView reward_gridView;
        private TextView tv_reward_num,tv_reward_more;


        public RewardViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            ll_reward = (RelativeLayout) view.findViewById(R.id.ll_reward);
            reward_gridView = (UnableScrollGridView) view.findViewById(R.id.reward_gridView);
            tv_reward_num = (TextView) view.findViewById(R.id.tv_reward_num);
            tv_reward_more = (TextView) view.findViewById(R.id.tv_reward_more);

            rewardAdapter = new RewardAdapter(mContext);
            reward_gridView.setAdapter(rewardAdapter);
        }
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class EmptyViewHolder extends RViewHolder {
        private ImageView imageView_empty;

        public EmptyViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            imageView_empty = (ImageView) view.findViewById(R.id.imageView_empty);

        }
    }


}
